//添加职员
import React, {Component} from 'react';
import app from '../app';
import {
    Form,
    Input,
    Button,
    Table,
    Modal,
    Row,
    Upload,
    Icon,
    Checkbox,
    Select,
    DatePicker,
    Pagination,
    Radio,
    TreeSelect
} from 'antd';

import {project, admin, menu, goods, rotation, type, invitation, coupon, user, supplier, sku} from '../value';
import {config} from "../config";
import moment from 'moment';

import {EditorState, convertToRaw, ContentState} from 'draft-js';
import {Editor} from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';
import Col from "antd/es/grid/col";
import '../css/component.css';
import md5 from 'MD5';
import {PictureUpload} from '../components/pictureUpload';

import tt from '../image/tt.jpg';


const FormItem = Form.Item;
const Option = Select.Option;
const RangePicker = DatePicker.RangePicker;
const TextArea = Input.TextArea;
const RadioGroup = Radio.Group;
const SHOW_PARENT = TreeSelect.SHOW_PARENT;

class AddAccount extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginTop: '10px'}}>添加账号</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="添加账号"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"编号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_number', {
                                 rules: [{required: true, message: '请输入编号'}],
                             })(
                                 <Input
                                     placeholder="请输入编号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"用户名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_nickname', {
                                 rules: [{required: true, message: '请输入用户名'}],
                             })(
                                 <Input
                                     placeholder="请输入用户名" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"账号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_username', {
                                 rules: [{required: true, message: '请输入账号'}],
                             })(
                                 <Input
                                     placeholder="请输入账号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"密码"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_password', {
                                 rules: [{required: true, message: '请输入密码'}],
                             })(
                                 <Input
                                     placeholder="请输入密码" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"类型"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_type', {
                                 rules: [{required: true, message: '请输入密码'}],
                             })(
                                 <Select placeholder="请选择类型" style={{width: '200px'}}>
                                     <Option value={'1'}>管理员</Option>
                                 </Select>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                values.admin_password = md5(values.admin_password);
                app.post(admin.CREATE_ADMIN, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'info', 2);
                    }
                })
            }
        })
    }

}

const AddAccounts = Form.create()(AddAccount);

//修改账号
class EditAccount extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginLeft: '20px'}}>修改账号</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="修改账号"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"编号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_number', {
                                 rules: [{required: true, message: '请输入编号'}],
                             })(
                                 <Input
                                     placeholder="请输入编号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"用户名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_nickname', {
                                 rules: [{required: true, message: '请输入用户名'}],
                             })(
                                 <Input
                                     placeholder="请输入用户名" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"账号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_username', {
                                 rules: [{required: true, message: '请输入账号'}],
                             })(
                                 <Input
                                     placeholder="请输入账号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"密码"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_password')(
                                 <Input
                                     placeholder="请输入密码" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"类型"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('admin_type', {
                                 rules: [{required: true, message: '请输入密码'}],
                             })(
                                 <Select placeholder="请选择类型" style={{width: '200px'}}>
                                     <Option value={'1'}>管理员</Option>
                                 </Select>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            const data = this.props.data;
            const formData = [];
            formData['admin_number'] = {value: data.admin_number};
            formData['admin_nickname'] = {value: data.admin_nickname};
            formData['admin_username'] = {value: data.admin_username};
            formData['admin_password'] = {value: data.admin_password};
            formData['admin_type'] = {value: data.admin_type};
            this.props.form.setFields(formData);
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (values.admin_password && values.admin_password.length > 0) {
                    values.admin_password = md5(values.admin_password);
                }
                values.admin_id = _this.props.data.admin_id;
                app.post(admin.UPDATE_ADMIN, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'info', 2);
                    }
                })
            }
        })
    }

}

const EditAccounts = Form.create()(EditAccount);

//根据最顶层的数据所含的子数据的ID和所有的分类ID比较，相同的则为他的子数据  循环权限
const loop = (menu, data) => {
    let menuList = [];
    data.map((item, index) => {
        if (menu.menu_id === item.menu_parentid) {
            menuList.push(item);
            menu.subList = menuList;
            loop(item, data);
        } else {
            menuList = [];
        }
    })
};

const loopType = (type, data) => {
    let typeList = [];
    data.map((item, index) => {
        if (type.type_id === item.type_parentId) {
            item.label = item.type_name;
            item.value = item.type_id;
            item.key = item.type_id;
            typeList.push(item);
            type.children = typeList;
            loopType(item, data);
        } else {

        }
    })
};

//增加职位
class AddPosition extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            newContent: '',
            data: [
                {
                    id: '01',
                    name: '设置',
                    parent_id: '0',
                    is_second: false,
                    is_check: false,
                    level: 1,
                    sub_list: [
                        {
                            id: '0101',
                            name: '修改信息',
                            parent_id: '01',
                            is_check: false,
                            level: 2
                        },
                        {
                            id: '0102',
                            name: '修改密码',
                            parent_id: '01',
                            is_check: false,
                            level: 2
                        }
                    ]
                },
                {
                    id: '02',
                    name: '职位管理',
                    parent_id: '0',
                    is_second: true,
                    is_check: false,
                    level: 1,
                    sub_list: [
                        {
                            id: '0201',
                            name: '新增职位',
                            parent_id: '02',
                            is_check: false,
                            level: 2,
                            sub_list: [
                                {
                                    id: '020101',
                                    name: '修改信息',
                                    parent_id: '0201',
                                    is_check: false,
                                    level: 3,
                                    sub_list: []
                                },
                                {
                                    id: '020102',
                                    name: '修改信息',
                                    parent_id: '0201',
                                    is_check: false,
                                    level: 3,
                                    sub_list: []
                                }
                            ]
                        },
                        {
                            id: '0202',
                            name: '修改密码',
                            parent_id: '02',
                            is_check: false,
                            level: 2,
                            sub_list: [
                                {
                                    id: '020201',
                                    name: '修改信息',
                                    parent_id: '0202',
                                    is_check: false,
                                    level: 2,
                                    sub_list: []
                                },
                                {
                                    id: '020202',
                                    name: '修改信息',
                                    parent_id: '0202',
                                    is_check: false,
                                    level: 2,
                                    sub_list: []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }

    formItemLayout = {
        labelCol: {span: 2},
        wrapperCol: {span: 18},
    };

    /**
     * 生成权限操作区域
     */
    genCheckbox = (data = []) => {
        return (
            data.map((item, index) => {
                return (
                    <Row style={{padding: 5, background: '#ddd', marginTop: 2}} key={'one' + index}>
                        <Col span={5}>
                            <Checkbox checked={item.is_check}
                                      onChange={(e) => this.handleFirstOnChange(e, item.id, item.level, item.parent_id, index)}>{item.name}</Checkbox>
                        </Col>
                        <Col span={19}>
                            {
                                item.is_second ?
                                    item.sub_list.map((item1, index1) => {
                                        return (
                                            <Row style={{padding: 5, background: '#f1f1f1'}}
                                                 key={'second' + index1}>
                                                <Col span={6}>
                                                    <Checkbox checked={item1.is_check}
                                                              onChange={(e) => this.handleSecondOnChange(e, item1.id, item1.level, item1.parent_id, index, index1)}>{item1.name}</Checkbox>
                                                </Col>
                                                <Col span={18}
                                                     style={{padding: 5, background: '#f7f7f7'}}>
                                                    {
                                                        item1.sub_list.map((item2, index2) => {
                                                            return (
                                                                <Checkbox checked={item2.is_check}
                                                                          key={'three' + index2}
                                                                          onChange={(e) => this.handleThreeOnChange(e, item2.id, item2.level, item2.parent_id, index, index1, index2)}>{item2.name}</Checkbox>
                                                            )
                                                        })
                                                    }
                                                </Col>
                                            </Row>
                                        )
                                    })
                                    :
                                    <Row style={{padding: 5, background: '#f7f7f7'}}>
                                        {
                                            item.sub_list.map((item1, index1) => {
                                                    return (

                                                        <Checkbox checked={item1.is_check}
                                                                  key={'second' + index1}
                                                                  onChange={(e) => this.handleSecondOnChange(e, item1.id, item1.level, item1.parent_id, index, index1)}>{item1.name}</Checkbox>

                                                    )
                                                }
                                            )
                                        }
                                    </Row>
                            }
                        </Col>
                    </Row>
                )
            })
        )
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>增加职位</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="增加职位"
                       okText={"提交"}
                       width={'1000px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <Row>
                        <FormItem label={"职位名称"} style={{
                            display: 'flex',
                            border: '1px solid #ddd',
                            marginBottom: 0
                        }} {...this.formItemLayout}>
                             {getFieldDecorator('project_name', {
                                 rules: [{required: true, message: '请输入职位名称'}],
                             })(
                                 <Input style={{marginLeft: 50}}
                                        placeholder="请输入职位名称"/>
                             )}
                         </FormItem>
                    <FormItem label={"职位说明"}
                              style={{display: 'flex', border: '1px solid #ddd'}} {...this.formItemLayout}>
                             {getFieldDecorator('project_name', {
                                 rules: [{required: true, message: '请输入职位说明'}],
                             })(
                                 <Input style={{marginLeft: 50}}
                                        placeholder="请输入职位说明"/>
                             )}
                         </FormItem>
                    </Row>
                    <Row>
                        {
                            this.genCheckbox(this.state.data)
                        }
                    </Row>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            this.handleSearchMenuList();
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        const {imgUrl, newContent} = this.state;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (newContent.length <= 0) {
                    app.alert('请输入简介内容', 'error', 2);
                    return;
                }
                values.project_picture = imgUrl;
                values.project_content = newContent;
                values.project_id = _this.props.project_id;
                app.post(project.UPDATE_PROJECT, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 2);
                        _this.setState({
                            visible: false,
                            imgUrl: '',
                            newContent: '',
                            editorState: EditorState.createEmpty()
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 处理一级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     */
    handleFirstOnChange(e, id, level, parent_id, index) {
        const check_status = e.target.checked;
        let data = this.state.data;
        data[index].is_check = check_status;
        let sub_list = data[index].sub_list;
        if (sub_list && sub_list.length > 0) {
            for (let i = 0; i < sub_list.length; i++) {
                sub_list[i].is_check = check_status;
                let sub_list1 = sub_list[i].sub_list;
                if (sub_list1 && sub_list1.length > 0) {
                    for (let j = 0; j < sub_list1.length; j++) {
                        sub_list1[j].is_check = check_status;
                    }
                }
            }
        }
        this.setState({data});
    }

    /**
     * 处理二级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     * @param index1
     */
    handleSecondOnChange(e, id, level, parent_id, index, index1) {
        console.log(index, index1);
        const check_status = e.target.checked;
        let data = this.state.data;
        if (check_status) {
            data[index].is_check = check_status;
        }
        data[index].sub_list[index1].is_check = check_status;
        let sub_list = data[index].sub_list[index1].sub_list;
        if (sub_list && sub_list.length > 0) {
            for (let i = 0; i < sub_list.length; i++) {
                data[index].sub_list[index1].sub_list[i].is_check = check_status;
            }
        }
        this.setState({data});
        if (!check_status) {
            if (!this.checkFirst(index)) {
                data[index].is_check = check_status;
            }
        }
        this.setState({data});
    }

    /**
     * 处理三级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     * @param index1
     * @param index2
     */
    handleThreeOnChange(e, id, level, parent_id, index, index1, index2) {
        console.log(index, index1, index2);
        const check_status = e.target.checked;
        let data = this.state.data;
        if (check_status) {
            data[index].is_check = check_status;
            data[index].sub_list[index1].is_check = check_status;
        }
        data[index].sub_list[index1].sub_list[index2].is_check = check_status;
        this.setState({data});
        if (!check_status) {
            if (!this.checkSecond(index, index1, index2)) {
                data[index].sub_list[index1].is_check = check_status;
            }
        }
        this.setState({data});
        if (!check_status) {
            if (!this.checkSecond1(index, index1, index2)) {
                data[index].is_check = check_status;
            }
        }
        this.setState({data});
    }

    /**
     * 检查下级菜单是否有被选中
     */
    checkFirst(index) {
        let data = this.state.data;
        let sub_list = data[index].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
                break;
            }
        }
        return is_check;
    }

    /**
     * 处理二级菜单是否被选中
     * @param index
     * @param index1
     * @param index2
     */
    checkSecond(index, index1, index2) {
        let data = this.state.data;
        let sub_list = data[index].sub_list[index1].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
            }
        }
        return is_check;
    }

    /**
     * 处理二级菜单是否被选中
     * @param index
     * @param index1
     * @param index2
     */
    checkSecond1(index, index1, index2) {
        let data = this.state.data;
        let sub_list = data[index].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
            }
            let sub_list1 = sub_list[i].sub_list;
            for (let j = 0; j < sub_list1.length; j++) {
                if (sub_list1[j].is_check) {
                    is_check = true;
                    break;
                }
            }
            if (is_check) {
                break;
            }
        }
        return is_check;
    }


    // 搜索所有菜单
    handleSearchMenuList = () => {
        this.setState({
            loading: true
        });
        app.post(menu.SEARCH_MENU_LIST_ALL, {
            menu_type: 1
        }).then(req => {
            this.setState({
                loading: false
            });
            if (req.code == 1) {
                let firstMenu = [];
                req.data.map((item) => {
                    if (item.menu_level === 1) {
                        firstMenu.push(item);
                    }
                });
                firstMenu.map((item) => {
                    loop(item, req.data);
                });
                this.setState({treeData: firstMenu});
                console.log(firstMenu);
            } else {
                this.setState({treeData: []});
                app.alert(req.msg, 'error', 3);
            }
        })
    };

}

const AddPositions = Form.create()(AddPosition);

//修改职位
class EditPosition extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            newContent: '',
            data: [
                {
                    id: '01',
                    name: '设置',
                    parent_id: '0',
                    is_second: false,
                    is_check: false,
                    level: 1,
                    sub_list: [
                        {
                            id: '0101',
                            name: '修改信息',
                            parent_id: '01',
                            is_check: false,
                            level: 2
                        },
                        {
                            id: '0102',
                            name: '修改密码',
                            parent_id: '01',
                            is_check: false,
                            level: 2
                        }
                    ]
                },
                {
                    id: '02',
                    name: '职位管理',
                    parent_id: '0',
                    is_second: true,
                    is_check: false,
                    level: 1,
                    sub_list: [
                        {
                            id: '0201',
                            name: '新增职位',
                            parent_id: '02',
                            is_check: false,
                            level: 2,
                            sub_list: [
                                {
                                    id: '020101',
                                    name: '修改信息',
                                    parent_id: '0201',
                                    is_check: false,
                                    level: 3,
                                    sub_list: []
                                },
                                {
                                    id: '020102',
                                    name: '修改信息',
                                    parent_id: '0201',
                                    is_check: false,
                                    level: 3,
                                    sub_list: []
                                }
                            ]
                        },
                        {
                            id: '0202',
                            name: '修改密码',
                            parent_id: '02',
                            is_check: false,
                            level: 2,
                            sub_list: [
                                {
                                    id: '020201',
                                    name: '修改信息',
                                    parent_id: '0202',
                                    is_check: false,
                                    level: 2,
                                    sub_list: []
                                },
                                {
                                    id: '020202',
                                    name: '修改信息',
                                    parent_id: '0202',
                                    is_check: false,
                                    level: 2,
                                    sub_list: []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }

    formItemLayout = {
        labelCol: {span: 2},
        wrapperCol: {span: 18},
    };

    /**
     * 生成权限操作区域
     */
    genCheckbox = (data = []) => {
        return (
            data.map((item, index) => {
                return (
                    <Row style={{padding: 5, background: '#d0d0ff', marginTop: 2}} key={'one' + index}>
                        <Col span={5}>
                            <Checkbox checked={item.is_check}
                                      onChange={(e) => this.handleFirstOnChange(e, item.id, item.level, item.parent_id, index)}>{item.name}</Checkbox>
                        </Col>
                        <Col span={19}>
                            {
                                item.is_second ?
                                    item.sub_list.map((item1, index1) => {
                                        return (
                                            <Row style={{padding: 5, background: '#d1e8ff'}}
                                                 key={'second' + index1}>
                                                <Col span={6}>
                                                    <Checkbox checked={item1.is_check}
                                                              onChange={(e) => this.handleSecondOnChange(e, item1.id, item1.level, item1.parent_id, index, index1)}>{item1.name}</Checkbox>
                                                </Col>
                                                <Col span={18}
                                                     style={{padding: 5, background: '#ffd1d1'}}>
                                                    {
                                                        item1.sub_list.map((item2, index2) => {
                                                            return (
                                                                <Checkbox checked={item2.is_check}
                                                                          key={'three' + index2}
                                                                          onChange={(e) => this.handleThreeOnChange(e, item2.id, item2.level, item2.parent_id, index, index1, index2)}>{item2.name}</Checkbox>
                                                            )
                                                        })
                                                    }
                                                </Col>
                                            </Row>
                                        )
                                    })
                                    :
                                    <Row style={{padding: 5, background: '#ffd1d1'}}>
                                        {
                                            item.sub_list.map((item1, index1) => {
                                                    return (

                                                        <Checkbox checked={item1.is_check}
                                                                  key={'second' + index1}
                                                                  onChange={(e) => this.handleSecondOnChange(e, item1.id, item1.level, item1.parent_id, index, index1)}>{item1.name}</Checkbox>

                                                    )
                                                }
                                            )
                                        }
                                    </Row>
                            }
                        </Col>
                    </Row>
                )
            })
        )
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>增加职位</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="增加职位"
                       okText={"提交"}
                       width={'1000px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <Row>
                        <FormItem label={"职位名称"} style={{
                            display: 'flex',
                            border: '1px solid #ddd',
                            marginBottom: 0
                        }} {...this.formItemLayout}>
                             {getFieldDecorator('project_name', {
                                 rules: [{required: true, message: '请输入职位名称'}],
                             })(
                                 <Input style={{marginLeft: 50}}
                                        placeholder="请输入职位名称"/>
                             )}
                         </FormItem>
                    <FormItem label={"职位说明"}
                              style={{display: 'flex', border: '1px solid #ddd'}} {...this.formItemLayout}>
                             {getFieldDecorator('project_name', {
                                 rules: [{required: true, message: '请输入职位说明'}],
                             })(
                                 <Input style={{marginLeft: 50}}
                                        placeholder="请输入职位说明"/>
                             )}
                         </FormItem>
                    </Row>
                    <Row>
                        {
                            this.genCheckbox(this.state.data)
                        }
                    </Row>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            //this.searchProjectById();
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        const {imgUrl, newContent} = this.state;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (newContent.length <= 0) {
                    app.alert('请输入简介内容', 'error', 2);
                    return;
                }
                values.project_picture = imgUrl;
                values.project_content = newContent;
                values.project_id = _this.props.project_id;
                app.post(project.UPDATE_PROJECT, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 2);
                        _this.setState({
                            visible: false,
                            imgUrl: '',
                            newContent: '',
                            editorState: EditorState.createEmpty()
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 处理一级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     */
    handleFirstOnChange(e, id, level, parent_id, index) {
        const check_status = e.target.checked;
        let data = this.state.data;
        data[index].is_check = check_status;
        let sub_list = data[index].sub_list;
        if (sub_list && sub_list.length > 0) {
            for (let i = 0; i < sub_list.length; i++) {
                sub_list[i].is_check = check_status;
                let sub_list1 = sub_list[i].sub_list;
                if (sub_list1 && sub_list1.length > 0) {
                    for (let j = 0; j < sub_list1.length; j++) {
                        sub_list1[j].is_check = check_status;
                    }
                }
            }
        }
        this.setState({data});
    }

    /**
     * 处理二级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     * @param index1
     */
    handleSecondOnChange(e, id, level, parent_id, index, index1) {
        console.log(index, index1);
        const check_status = e.target.checked;
        let data = this.state.data;
        if (check_status) {
            data[index].is_check = check_status;
        }
        data[index].sub_list[index1].is_check = check_status;
        let sub_list = data[index].sub_list[index1].sub_list;
        if (sub_list && sub_list.length > 0) {
            for (let i = 0; i < sub_list.length; i++) {
                data[index].sub_list[index1].sub_list[i].is_check = check_status;
            }
        }
        this.setState({data});
        if (!check_status) {
            if (!this.checkFirst(index)) {
                data[index].is_check = check_status;
            }
        }
        this.setState({data});
    }

    /**
     * 处理三级菜单选择变化事件
     * @param e
     * @param id
     * @param level
     * @param parent_id
     * @param index
     * @param index1
     * @param index2
     */
    handleThreeOnChange(e, id, level, parent_id, index, index1, index2) {
        console.log(index, index1, index2);
        const check_status = e.target.checked;
        let data = this.state.data;
        if (check_status) {
            data[index].is_check = check_status;
            data[index].sub_list[index1].is_check = check_status;
        }
        data[index].sub_list[index1].sub_list[index2].is_check = check_status;
        this.setState({data});
        if (!check_status) {
            if (!this.checkSecond(index, index1, index2)) {
                data[index].sub_list[index1].is_check = check_status;
            }
        }
        this.setState({data});
        if (!check_status) {
            if (!this.checkSecond1(index, index1, index2)) {
                data[index].is_check = check_status;
            }
        }
        this.setState({data});
    }

    /**
     * 检查下级菜单是否有被选中
     */
    checkFirst(index) {
        let data = this.state.data;
        let sub_list = data[index].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
                break;
            }
        }
        return is_check;
    }

    /**
     * 处理二级菜单是否被选中
     * @param index
     * @param index1
     * @param index2
     */
    checkSecond(index, index1, index2) {
        let data = this.state.data;
        let sub_list = data[index].sub_list[index1].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
            }
        }
        return is_check;
    }

    /**
     * 处理二级菜单是否被选中
     * @param index
     * @param index1
     * @param index2
     */
    checkSecond1(index, index1, index2) {
        let data = this.state.data;
        let sub_list = data[index].sub_list;
        let is_check = false;
        for (let i = 0; i < sub_list.length; i++) {
            if (sub_list[i].is_check) {
                is_check = true;
            }
            let sub_list1 = sub_list[i].sub_list;
            for (let j = 0; j < sub_list1.length; j++) {
                if (sub_list1[j].is_check) {
                    is_check = true;
                    break;
                }
            }
            if (is_check) {
                break;
            }
        }
        return is_check;
    }
}

const EditPositions = Form.create()(EditPosition);

class AddAdvert extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            pictureList: [],
            pictureList1: [],
            goodsInfo: {}
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginTop: '10px'}}>新增</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="新增"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"标题"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_name', {
                                 rules: [{required: true, message: '请输入标题'}],
                             })(
                                 <Input
                                     placeholder="请输入标题" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"图片"} style={{display: 'flex'}} {...this.formItemLayout}>
                    {getFieldDecorator('project_picture')(
                        <PictureUpload pictureList={this.state.pictureList} len={1}
                                       pictureList1={this.state.pictureList1}
                                       getPictureList={(data, data1) => this.handleGetPictureList(data, data1)}></PictureUpload>
                    )}
                    </FormItem>
                    <FormItem label={"跳转页面"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_type', {
                                 rules: [{required: true, message: '请选择跳转页面'}],
                             })(
                                 <Select style={{width: 200}} placeholder={'请选择跳转页面'}>
                                     <Option value="1">商品页面</Option>
                                     <Option value="2">推广预约</Option>
                                     <Option value="3">充值</Option>
                                     <Option value="4">会员卡</Option>
                                     <Option value="5">意见反馈</Option>
                                 </Select>
                             )}
                         </FormItem>
                    <FormItem label={"选择商品"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('goods_code')(
                                 <Input
                                     placeholder="请输入商品编码" style={{width: '200px'}}
                                     onBlur={(e) => this.handleOnBlur(e)}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_explain')(
                                 <Input
                                     placeholder="请输入说明" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"排序"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_sort', {
                                 rules: [{required: true, message: '请输入序号'}],
                             })(
                                 <Input
                                     placeholder="请输入序号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        const {goodsInfo, pictureList1} = this.state;
        if (pictureList1.length <= 0) {
            app.alert('请上传图片', 'info', 1);
            return;
        }
        this.props.form.validateFields((err, values) => {
            if (!err) {
                values.goods_id = goodsInfo.goods_id;
                values.file_path = pictureList1[0].file_path;
                values.file_name = pictureList1[0].file_name;
                app.post(rotation.ADD_ROTATION, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 上传文件
     * @param file
     * @param fileList
     * @param event
     */
    handleUpload(event) {
        const file = event.file;
        const status = file.status;
        const _this = this;
        if (status === "done") {
            const response = file.response;
            if (response.code === 1) {
                const imgUrl = response.data.imgName;
                _this.setState({imgUrl});
            } else {
                app.alert(response.msg, 'error', 1);
                _this.setState({imgUrl: ''});
            }
        }
    }

    /**
     * 获取图片
     * @param pictureList
     */
    handleGetPictureList(pictureList, pictureList1) {
        this.setState({pictureList, pictureList1});
    }

    /**
     * 处理输入框离开事件
     * @param e
     */
    handleOnBlur(e) {
        console.log(e.target.value);
        const goods_code = e.target.value;
        app.post(goods.SEARCH_GOODS_ID_BY_GOODS_CODE, {goods_code}).then((res) => {
            if (res.code === 1) {
                this.setState({goodsInfo: {goods_id: res.data.goods_id, goods_name: res.data.goods_name}});
            } else {
                this.setState({goodsInfo: {}});
                app.alert(res.msg, 'info', 1);
            }
        })
    }

}

const AddAdverts = Form.create()(AddAdvert);

class EditAdvert extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            pictureList: []
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginTop: '10px'}}>编辑</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="编辑"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"标题"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_name', {
                                 rules: [{required: true, message: '请输入标题'}],
                             })(
                                 <Input
                                     placeholder="请输入标题" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"图片"} style={{display: 'flex'}} {...this.formItemLayout}>
                    {getFieldDecorator('project_picture')(
                        <PictureUpload pictureList={this.state.pictureList} len={1}
                                       pictureList1={this.state.pictureList1}
                                       getPictureList={(data, data1) => this.handleGetPictureList(data, data1)}></PictureUpload>
                    )}
                    </FormItem>
                    <FormItem label={"跳转页面"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_type', {
                                 rules: [{required: true, message: '请选择跳转页面'}],
                             })(
                                 <Select style={{width: 200}} placeholder={'请选择跳转页面'}>
                                     <Option value="1">商品页面</Option>
                                     <Option value="2">推广预约</Option>
                                     <Option value="3">充值</Option>
                                     <Option value="4">会员卡</Option>
                                     <Option value="5">意见反馈</Option>
                                 </Select>
                             )}
                         </FormItem>
                    <FormItem label={"选择商品"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('goods_code')(
                                 <Input
                                     placeholder="请输入商品编码" style={{width: '200px'}}
                                     onBlur={(e) => this.handleOnBlur(e)}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_explain')(
                                 <Input
                                     placeholder="请输入说明" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"排序"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('rotation_sort', {
                                 rules: [{required: true, message: '请输入序号'}],
                             })(
                                 <Input
                                     placeholder="请输入序号" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            const {rotation_name, file_path, file_name, goods_code, rotation_explain, rotation_sort, rotation_type, goods_id} = this.props.data;
            const formData = [];
            formData['rotation_name'] = {value: rotation_name};
            formData['goods_code'] = {value: goods_code};
            formData['rotation_explain'] = {value: rotation_explain};
            formData['rotation_sort'] = {value: rotation_sort};
            formData['rotation_type'] = {value: rotation_type};
            const pictureList = [file_path];
            const pictureList1 = [{file_name: file_name, file_path: file_path}];
            const goodsInfo = {goods_id: goods_id, goods_name: ''};
            this.setState({pictureList1, pictureList, goodsInfo});
            this.props.form.setFields(formData);
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        const {goodsInfo, pictureList1} = this.state;
        if (pictureList1.length <= 0) {
            app.alert('请上传图片', 'info', 1);
            return;
        }
        this.props.form.validateFields((err, values) => {
            if (!err) {
                values.goods_id = goodsInfo.goods_id;
                values.file_path = pictureList1[0].file_path;
                values.file_name = pictureList1[0].file_name;
                values.rotation_id = this.props.data.rotation_id;
                app.post(rotation.UPDATE_ROTATION, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 上传文件
     * @param file
     * @param fileList
     * @param event
     */
    handleUpload(event) {
        const file = event.file;
        const status = file.status;
        const _this = this;
        if (status === "done") {
            const response = file.response;
            if (response.code === 1) {
                const imgUrl = response.data.imgName;
                _this.setState({imgUrl});
            } else {
                app.alert(response.msg, 'error', 1);
                _this.setState({imgUrl: ''});
            }
        }
    }

    /**
     * 获取图片
     * @param pictureList
     */
    handleGetPictureList(pictureList, pictureList1) {
        this.setState({pictureList, pictureList1});
    }

    /**
     * 处理输入框离开事件
     * @param e
     */
    handleOnBlur(e) {
        console.log(e.target.value);
        const goods_code = e.target.value;
        app.post(goods.SEARCH_GOODS_ID_BY_GOODS_CODE, {goods_code}).then((res) => {
            if (res.code === 1) {
                this.setState({goodsInfo: {goods_id: res.data.goods_id, goods_name: res.data.goods_name}});
            } else {
                this.setState({goodsInfo: {}});
                app.alert(res.msg, 'info', 1);
            }
        })
    }

}

const EditAdverts = Form.create()(EditAdvert);

class ChooseGood extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            optionValue: [
                {
                    id: '01',
                    name: '素食'
                },
                {
                    id: '02',
                    name: '肉食'
                }
            ],
            goodList: [],
            chooseData: [],
            selectShopType: [],
            goods_name: ''
        }
    }

    render() {
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>去选择</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="选择商品"
                       okText={"提交"}
                       width={'800px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <Row style={{display: 'flex'}}>
                        <div>
                            商品名称：<Input placeholder="请输入商品名称" style={{width: '200px'}}
                                        onChange={(e) => this.handleNameOnChange(e)}
                                        onPressEnter={() => this.handleOnPressEnter()}
                        />
                        </div>
                        <div style={{marginLeft: 10}}>
                            商品分类：
                        <TreeSelect
                            treeData={this.state.shopType}
                            treeCheckable={true}
                            multiple={true}
                            onSelect={this.getShopType}
                            showCheckedStrategy={SHOW_PARENT}
                            searchPlaceholder='请选择'
                            style={{width: 200}}
                        />
                        </div>
                        <Button type={'primary'} style={{marginLeft: 20}}
                                onClick={() => this.handleSearchBtn()}>搜索</Button>
                    </Row>
                    <Row className={'clearfix'}>
                        {
                            this.state.goodList.map((item, index) => {
                                return (
                                    <div className={'goodItem'} key={index}>
                                        <img src={config.imgServerUrl + item.file_path}></img>
                                        <div className={'goodPrice'}>
                                            <div className={'goodPriceLeft'}>{item.goods_price}</div>
                                            <div className={'goodPriceRight'}><Checkbox checked={item.checked}
                                                                                        onChange={() => this.handleOnChange(item.goods_id)}></Checkbox>
                                            </div>
                                        </div>
                                        <div style={{marginTop: 5, overflow: 'hidden'}}>
                                            {item.goods_name}
                                        </div>
                                    </div>
                                )
                            })
                        }
                    </Row>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            let chooseData = this.props.data;
            let {activity_id, operateType} = this.props;
            this.setState({chooseData, operateType, activity_id});

            if (operateType === 1) {
                this.handleSearchGoodList({}, chooseData);
            } else {
                this.handleSearchGoodList({activity_id}, chooseData);
            }
            this.requestShopType()
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let chooseData = this.state.chooseData;
        this.props.handleChoose(chooseData);
        this.showModal(false);
    }

    /**
     * 搜索商品
     * @param param
     * @param chooseData
     */
    handleSearchGoodList(param, chooseData) {
        let _this = this;
        _this.setState({loading: true});
        app.post(goods.SEARCH_GOODS_BY_ACTIVITY, param).then(result => {
            if (result.code === 1) {
                const data = result.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].checked = false;
                    for (let j = 0; j < chooseData.length; j++) {
                        if (data[i].goods_id === chooseData[j].goods_id) {
                            data[i].checked = true;
                            break;
                        }
                    }
                }
                this.setState({goodList: data, goodLists: data, loading: false});
            } else {
                app.alert(result.msg, 'success', 2);
                this.setState({goodList: [], goodLists: [], loading: false});
            }
        });
    }

    /**
     * 处理选择变化事件
     * @param id
     */
    handleOnChange(id) {
        let {goodList, chooseData} = this.state;
        for (let i = 0; i < goodList.length; i++) {
            if (goodList[i].goods_id === id) {
                goodList[i].checked = !goodList[i].checked;
                if (goodList[i].checked) {
                    chooseData.push(goodList[i]);
                } else {
                    const index = chooseData.indexOf(goodList[i]);
                    chooseData.splice(index, 1);
                }
            }
        }
        console.log(chooseData);
        this.setState({goodList: goodList, chooseData});
    }

    /**
     * 处理商品名输入变化事件
     * @param e
     */
    handleNameOnChange(e) {
        this.setState({goods_name: e.target.value});
    };

    /**
     * 回车事件
     */
    handleOnPressEnter() {
        let {selectShopType, activity_id, operateType, goods_name, chooseData} = this.state;
        let param = {};
        let typeSearch = "";
        for (let i = 0; i < selectShopType.length; i++) {
            typeSearch += selectShopType[i].type_id + ',';
        }
        if (typeSearch.length > 0) {
            typeSearch = typeSearch.substr(0, typeSearch.length - 1);
        }
        if (operateType === 1) {
            param.goodsSearch = goods_name;
            param.typeSearch = typeSearch;
        } else if (operateType === 2) {
            param.goodsSearch = goods_name;
            param.typeSearch = typeSearch;
            param.activity_id = activity_id;
        }
        this.handleSearchGoodList(param, chooseData);
    }

    handleSearchBtn() {
        let {selectShopType, activity_id, operateType, goods_name, chooseData} = this.state;
        let param = {};
        let typeSearch = "";
        for (let i = 0; i < selectShopType.length; i++) {
            typeSearch += selectShopType[i].type_id + ',';
        }
        if (typeSearch.length > 0) {
            typeSearch = typeSearch.substr(0, typeSearch.length - 1);
        }
        if (operateType === 1) {
            param.goodsSearch = goods_name;
            param.typeSearch = typeSearch;
        } else if (operateType === 2) {
            param.goodsSearch = goods_name;
            param.typeSearch = typeSearch;
            param.activity_id = activity_id;
        }
        this.handleSearchGoodList(param, chooseData);
    };

    /**
     * 获取分类
     */
    requestShopType() {
        let msg = {};
        let temp;
        app.post(type.SEARCH_ALL_TYPE, {}).then(req => {
            if (req.code == 1) {
                let firstMenu = [];
                req.data.map((item) => {
                    // 先查到最顶层数据
                    temp = {
                        label: item.type_name,
                        value: item.type_id,
                        type_id: item.type_id,
                        type_isable: item.type_isable,
                        type_level: item.type_level,
                        type_name: item.type_name,
                        type_parentId: item.type_parentId,
                        key: item.type_id
                    };
                    if (!item.type_parentId) {
                        firstMenu.push(temp);
                    }
                });
                firstMenu.map((item) => {
                    loopType(item, req.data);  //根据最顶层的数据所含的子数据的ID和所有的分类ID比较，相同的则为他的子数据
                });
                this.setState({shopType: firstMenu});
            } else if (req.msg == '查询无记录') {
                app.alert('请先添加分类', 'error', 3);
            } else if (req.code == 2) {
                app.alert(req.msg, 'error', 3)
            }
        });
    }

    // 获取商家选中的分类
    getShopType = (value, label, extra) => {
        console.log(value, label, extra);
        let selectShopType = [];
        let temp = {};
        extra.checkedNodes.forEach((item) => {
            temp = {
                type_id: item.props.type_id,
                type_level: item.props.type_level,
                type_name: item.props.type_name,
                type_parentId: item.props.type_parentId
            };
            selectShopType.push(temp);
        });
        console.log(selectShopType);
        this.setState({
            selectShopType: selectShopType  //复选框全部选中的店铺分类
        });
        let {goods_name, activity_id, operateType, chooseData} = this.state;
        let param = {};
        let typeSearch = "";
        for (let i = 0; i < selectShopType.length; i++) {
            typeSearch += selectShopType[i].type_id + ',';
        }
        if (typeSearch.length > 0) {
            typeSearch = typeSearch.substr(0, typeSearch.length - 1);
        }
        if (operateType === 1) {
            param = {
                goodsSearch: goods_name,
                typeSearch
            }
        } else if (operateType === 2) {
            param = {
                goodsSearch: goods_name,
                typeSearch,
                activity_id
            }
        }
        this.handleSearchGoodList(param, chooseData);
    };

}

const ChooseGoods = Form.create()(ChooseGood);


class AddInvitationActivity extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            invitationType: '2',//奖励类型
            optionValue: [
                {
                    id: '01',
                    name: '注册邀请奖励'
                },
                {
                    id: '02',
                    name: '登录邀请奖励'
                }
            ]
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        const dateFormat = 'YYYY-MM-DD';
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginLeft: '20px'}}>新建</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="新建"
                       okText={"提交"}
                       width={'600px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <FormItem label={"标题"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_name', {
                                 rules: [{required: true, message: '请输入标题'}],
                             })(
                                 <Input
                                     placeholder="请输入标题" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"类型"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_type', {
                                 rules: [{required: true, message: '请选择类型'}],
                             })(
                                 <Select style={{width: 200}} placeholder={'请选择类型'}
                                         onChange={(e) => this.handleChooseType(e)}>
                                     <Option value={'1'}>消费邀请奖励</Option>
                                     <Option value={'2'}>注册邀请奖励</Option>
                                 </Select>
                             )}
                         </FormItem>
                    {
                        this.state.invitationType === '1' ?
                            <FormItem label={"消费金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                                {getFieldDecorator('invitation_cost', {
                                    rules: [{required: true, message: '请输入消费金额'}],
                                })(
                                    <Input
                                        placeholder="请输入消费金额" style={{width: '200px'}} type={'number'}/>
                                )}
                            </FormItem> : ''
                    }
                    <FormItem label={"奖励金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_reward', {
                                 rules: [{required: true, message: '请输入奖励金额'}],
                             })(
                                 <Input
                                     placeholder="请输入奖励金额" style={{width: '200px'}} type={'number'}/>
                             )}
                         </FormItem>
                    <FormItem label={"开始结束时间"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('time', {
                                 rules: [{required: true, message: '请选择开始结束时间'}],
                             })(
                                 <RangePicker format={dateFormat} style={{width: 200}}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_explain')(
                                 <TextArea placeholder={'请输入说明'} style={{width: 200}}></TextArea>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                const time = values.time;
                values.invitation_startTime = moment(time[0]._d).format('YYYY-MM-DD HH:mm:ss');
                values.invitation_endTime = moment(time[1]._d).format('YYYY-MM-DD HH:mm:ss');
                app.post(invitation.ADD_INVITATION, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("添加成功", 'success', 1);
                        _this.setState({
                            visible: false,
                            invitationType: '2'
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        })
    }

    /**
     * 选择类型变化事件
     * @param e
     */
    handleChooseType(e) {
        this.setState({invitationType: e});
    }

}

const AddInvitationActivitys = Form.create()(AddInvitationActivity);

//修改账号
class EditInvitationActivity extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            invitationType: ''
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        const dateFormat = 'YYYY-MM-DD';
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>编辑</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="编辑"
                       okText={"提交"}
                       width={'600px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <FormItem label={"标题"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_name', {
                                 rules: [{required: true, message: '请输入标题'}],
                             })(
                                 <Input
                                     placeholder="请输入标题" style={{width: '200px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"类型"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_type', {
                                 rules: [{required: true, message: '请选择类型'}],
                             })(
                                 <Select style={{width: 200}} placeholder={'请选择类型'}
                                         onChange={(e) => this.handleChooseType(e)}>
                                     <Option value={'1'}>消费邀请奖励</Option>
                                     <Option value={'2'}>注册邀请奖励</Option>
                                 </Select>
                             )}
                         </FormItem>
                    {
                        this.state.invitationType === '1' ?
                            <FormItem label={"消费金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                                {getFieldDecorator('invitation_cost', {
                                    rules: [{required: true, message: '请输入消费金额'}],
                                })(
                                    <Input
                                        placeholder="请输入消费金额" style={{width: '200px'}} type={'number'}/>
                                )}
                            </FormItem> : ''
                    }
                    <FormItem label={"奖励金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_reward', {
                                 rules: [{required: true, message: '请输入奖励金额'}],
                             })(
                                 <Input
                                     placeholder="请输入奖励金额" style={{width: '200px'}} type={'number'}/>
                             )}
                         </FormItem>
                    <FormItem label={"开始结束时间"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('time', {
                                 rules: [{required: true, message: '请选择开始结束时间'}],
                             })(
                                 <RangePicker format={dateFormat} style={{width: 200}}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_explain')(
                                 <TextArea placeholder={'请输入说明'} style={{width: 200}}></TextArea>
                             )}
                         </FormItem>
                    <FormItem label={"状态"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('invitation_isable', {
                                 rules: [{required: true, message: '请选择状态'}],
                             })(
                                 <Select style={{width: 200}} placeholder={'请选择状态'}>
                                     <Option value={'1'}>启用</Option>
                                     <Option value={'0'}>停用</Option>
                                 </Select>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            let {invitation_name, invitation_type, invitation_reward, invitation_cost, invitation_startTime, invitation_endTime, invitation_explain, invitation_isable} = this.props.data;
            let dataFields = [];
            dataFields['invitation_name'] = {value: invitation_name};
            dataFields['invitation_type'] = {value: invitation_type};
            dataFields['invitation_reward'] = {value: invitation_reward};
            dataFields['time'] = {value: [moment(new Date(invitation_startTime), 'YYYY-MM-DD'), moment(new Date(invitation_endTime), 'YYYY-MM-DD')]};
            dataFields['invitation_explain'] = {value: invitation_explain};
            dataFields['invitation_isable'] = {value: invitation_isable};
            dataFields['invitation_cost'] = {value: invitation_cost};
            this.setState({invitationType: invitation_type});
            this.props.form.setFields(dataFields);
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                const time = values.time;
                values.invitation_startTime = moment(time[0]._d).format('YYYY-MM-DD HH:mm:ss');
                values.invitation_endTime = moment(time[1]._d).format('YYYY-MM-DD HH:mm:ss');
                values.invitation_id = _this.props.data.invitation_id;
                app.post(invitation.UPDATE_INVITATION, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 2);
                        _this.setState({
                            visible: false,
                            invitationType: '2'
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 选择类型变化事件
     * @param e
     */
    handleChooseType(e) {
        this.setState({invitationType: e});
    }

}

const EditInvitationActivitys = Form.create()(EditInvitationActivity);

//已邀请列表
class CustomerList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            data: [
                {
                    order_number: 1,
                    name: 'cbl',
                    type: 1,
                    people: '李三',
                    price: 12,
                    created_at: '2018-12-26'
                }
            ],
            newContent: ''
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number'
        },
        {
            title: '被邀请人',
            dataIndex: 'invitedUserName'
        },
        {
            title: '会员类型',
            dataIndex: 'invitedUserType',
            render: invitedUserType => {
                let text = '';
                if (invitedUserType === '1') {
                    text = '普通会员';
                } else if (invitedUserType === '2') {
                    text = 'VIP';
                }
                return (
                    <span>{text}</span>
                )
            }
        },
        {
            title: '邀请人',
            dataIndex: 'userName'
        },
        {
            title: '奖励金额',
            dataIndex: 'invitation_user_price'
        },
        {
            title: '注册时间',
            dataIndex: 'invitation_user_createTime',
            render: invitation_user_createTime => {
                return (
                    <span>{moment(invitation_user_createTime).format('YYYY-MM-DD')}</span>
                )
            }
        }
    ];

    render() {
        return (
            <span>
                <div onClick={() => this.showModal(true)}
                     style={{cursor: 'pointer', color: 'blue'}}>{this.props.num}</div>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="已邀请列表"
                       okText={"提交"}
                       width={'800px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <Row style={{marginTop: '20px'}}>
                    <Table
                        columns={this.columns}
                        rowKey={(record, index) => index}
                        dataSource={this.state.data}
                        loading={this.state.loading}
                        pagination={false}
                        onChange={this.handleTableChange}
                    />
                </Row>
                <Row style={{textAlign: 'right', marginTop: 20}}>
                    <Pagination {...this.state.pagination} showSizeChanger showQuickJumper
                                onChange={(page, pageSize) => this.handleOnChange(page, pageSize)}
                                onShowSizeChange={(current, size) => this.handleOnShowSizeChange(current, size)}/>
                </Row>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            const invitation_id = this.props.id;
            const param = {
                page: 1,
                size: 10,
                invitation_id
            };
            this.searchProject(param);
        }
        this.setState({visible: visible});
    }

    /**
     * 处理页码、显示数量变化事件
     * @param pagination
     * @param filters
     * @param sorter
     */
    handleTableChange = (pagination, filters, sorter) => {
        const current = pagination.current;
        const size = pagination.pageSize;
        const pagination1 = this.state.pagination;
        pagination1.current = current;
        pagination1.pageSize = size;
        this.setState({pagination: pagination1});
    };

    /**
     * 处理页面改变事件
     * @param page
     * @param pageSize
     */
    handleOnChange(page, pageSize) {
        const param = {
            page,
            size: pageSize,
            invitation_id: this.props.id
        };
        this.searchProject(param);
    }

    /**
     * 处理pageSize变化回调
     * @param current
     * @param size
     */
    handleOnShowSizeChange(current, size) {
        const param = {
            page: current,
            size,
            invitation_id: this.props.id
        };
        this.searchProject(param);
    }

    /**
     * 查找已邀请列表
     * @param param
     */
    searchProject(param) {
        let _this = this;
        _this.setState({loading: true});
        app.post(invitation.SEARCH_INVITATION_USER_LIST, param).then(result => {
            if (result.code === 1) {
                const data = result.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].order_number = i + 1;
                }
                const pagination = {total: result.count, current: param.page, pageSize: param.size};
                this.setState({pagination, data: data, loading: false});
            } else {
                app.alert(result.msg, 'success', 1);
                const pagination = {total: 0, current: 0, pageSize: 0};
                this.setState({pagination, data: [], loading: false});
            }
        });
    }

}

const CustomerLists = Form.create()(CustomerList);

//已邀请列表
class GoodList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            data: [],
            newContent: '',
            pagination: {current: 0, pageSize: 0, total: 0},
            typeSearch: ''
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number'
        },
        {
            title: '商品名',
            dataIndex: 'goods_name'
        },
        {
            title: '分类',
            dataIndex: 'goods_type'
        },
        {
            title: '库存',
            dataIndex: 'goods_stock'
        },
        {
            title: '售价',
            dataIndex: 'goods_price'
        },
        {
            title: '操作',
            dataIndex: this.columns,
            render: data => {
                return (
                    <span>{
                        data.hasIn ? data.isOld ?
                            <Button disabled={true}>已加入团购</Button> :
                            <Button type={'danger'} onClick={() => this.handleCancel(data.order_number)}>取消团购</Button>
                            :
                            <Button type={'primary'}
                                    onClick={() => this.handleTakePartIn(data.order_number)}>加入团购</Button>
                    }</span>
                )
            }
        }
    ];

    render() {
        return (
            <span>
                <Button type={'primary'} onClick={() => this.showModal(true)}
                >新增商品</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="商品选择"
                       okText={"提交"}
                       width={'800px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <Row>
                        分类：
                        <TreeSelect
                            treeData={this.state.shopType}
                            treeCheckable={true}
                            multiple={true}
                            onSelect={this.getShopType}
                            showCheckedStrategy={SHOW_PARENT}
                            searchPlaceholder='请选择'
                            style={{width: 200}}
                        />
                    </Row>
                         <Row style={{marginTop: '20px'}}>
                    <Table
                        columns={this.columns}
                        rowKey={(record, index) => index}
                        dataSource={this.state.data}
                        loading={this.state.loading}
                        pagination={false}
                        onChange={this.handleTableChange}
                    />
                </Row>
                <Row style={{textAlign: 'right', marginTop: 20}}>
                    <Pagination {...this.state.pagination} showSizeChanger showQuickJumper
                                onChange={(page, pageSize) => this.handleOnChange(page, pageSize)}
                                onShowSizeChange={(current, size) => this.handleOnShowSizeChange(current, size)}/>
                </Row>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            this.requestShopType();
            let {chooseData} = this.props;
            let param = {
                page: 1,
                size: 10
            };
            this.setState({oldChooseData: chooseData, newChooseData: []});
            this.searchGoodsList(param, chooseData, []);
        }
        this.setState({visible: visible});
    }

    /**
     * 处理页码、显示数量变化事件
     * @param pagination
     * @param filters
     * @param sorter
     */
    handleTableChange = (pagination, filters, sorter) => {
        const current = pagination.current;
        const size = pagination.pageSize;
        const pagination1 = this.state.pagination;
        pagination1.current = current;
        pagination1.pageSize = size;
        this.setState({pagination: pagination1});
    };

    /**
     * 处理页面改变事件
     * @param page
     * @param pageSize
     */
    handleOnChange(page, pageSize) {
        let {typeSearch, oldChooseData, newChooseData} = this.state;
        let param = {
            page,
            size: pageSize,
            typeSearch
        };
        this.searchGoodsList(param, oldChooseData, newChooseData);
    }

    /**
     * 处理pageSize变化回调
     * @param current
     * @param size
     */
    handleOnShowSizeChange(current, size) {
        let {typeSearch, oldChooseData, newChooseData} = this.state;
        let param = {
            page: current,
            size,
            typeSearch
        };
        this.searchGoodsList(param, oldChooseData, newChooseData);
    }

    /**
     * 查找商品列表
     * @param param
     * @param oldChooseData
     * @param newChooseData
     */
    searchGoodsList(param, oldChooseData, newChooseData) {
        let _this = this;
        _this.setState({loading: true});
        app.post(goods.SEARCH_GOODS_LIST, param).then(result => {
            if (result.code === 1) {
                const data = result.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].order_number = i + 1;
                    data[i].hasIn = false;
                    data[i].isOld = false;
                    for (let j = 0; j < oldChooseData.length; j++) {
                        if (data[i].goods_code === oldChooseData[j].goods_code) {
                            data[i].hasIn = true;
                            data[i].isOld = true;
                            break;
                        }
                    }
                    for (let j = 0; j < newChooseData.length; j++) {
                        if (data[i].goods_code === newChooseData[j].goods_code) {
                            data[i].hasIn = true;
                            data[i].isOld = false;
                            break;
                        }
                    }
                }
                const pagination = {total: data.length, current: param.page, pageSize: param.size};
                this.setState({pagination, data: data, loading: false});
            } else {
                app.alert(result.msg, 'success', 2);
                const pagination = {total: 0, current: 0, pageSize: 0};
                this.setState({pagination, data: [], loading: false});
            }
        });
    }

    /**
     * 搜索条件变化
     * @param e
     */
    handleParamType(e) {
        this.setState({paramType: e});
    }

    /**
     * 加入团购
     * @param data
     */
    handleTakePartIn(index) {
        let {data, newChooseData} = this.state;
        let item = data[index - 1];
        data[index - 1].hasIn = true;
        item.hasIn = true;
        newChooseData.push(item);
        this.setState({newChooseData, data});
    }

    /**
     * 取消团购
     * @param index
     */
    handleCancel(index) {
        let {data, newChooseData} = this.state;
        let item = data[index - 1];
        data[index - 1].hasIn = false;
        let currentIndex = newChooseData.indexOf(item);
        newChooseData.splice(currentIndex, 1);
        this.setState({newChooseData, data});
    }

    /**
     * 获取分类
     */
    requestShopType() {
        let msg = {};
        let temp;
        app.post(type.SEARCH_ALL_TYPE, {}).then(req => {
            if (req.code == 1) {
                let firstMenu = [];
                req.data.map((item) => {
                    // 先查到最顶层数据
                    temp = {
                        label: item.type_name,
                        value: item.type_id,
                        type_id: item.type_id,
                        type_isable: item.type_isable,
                        type_level: item.type_level,
                        type_name: item.type_name,
                        type_parentId: item.type_parentId,
                        key: item.type_id
                    };
                    if (!item.type_parentId) {
                        firstMenu.push(temp);
                    }
                });
                firstMenu.map((item) => {
                    loopType(item, req.data);  //根据最顶层的数据所含的子数据的ID和所有的分类ID比较，相同的则为他的子数据
                });
                this.setState({shopType: firstMenu});
            } else if (req.msg == '查询无记录') {
                app.alert('请先添加分类', 'error', 3);
            } else if (req.code == 2) {
                app.alert(req.msg, 'error', 3)
            }
        });
    }

    // 获取商家选中的分类
    getShopType = (value, label, extra) => {
        console.log(value, label, extra);
        let selectShopType = [];
        let temp = {};
        extra.checkedNodes.forEach((item) => {
            temp = {
                type_id: item.props.type_id,
                type_level: item.props.type_level,
                type_name: item.props.type_name,
                type_parentId: item.props.type_parentId
            };
            selectShopType.push(temp);
        });
        console.log(selectShopType);
        this.setState({
            selectShopType: selectShopType  //复选框全部选中的店铺分类
        });
        let {oldChooseData, newChooseData} = this.state;
        let typeSearch = "";
        for (let i = 0; i < selectShopType.length; i++) {
            typeSearch += selectShopType[i].type_id + ',';
        }
        if (typeSearch.length > 0) {
            typeSearch = typeSearch.substr(0, typeSearch.length - 1);
        }
        this.setState({typeSearch});
        let param = {
            page: 1,
            size: 10,
            typeSearch
        };
        this.searchGoodsList(param, oldChooseData, newChooseData);
    };

    /**
     * 提交商品
     */
    handleSubmit() {
        let newChooseData = this.state.newChooseData;
        if (newChooseData.length <= 0) {
            app.alert('请选择商品', 'info', 1);
            return;
        }
        this.props.handleChooseGoods(newChooseData);
        this.setState({visible: false});
    }

}

const GoodLists = Form.create()(GoodList);


class AddRedPacket extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            value: 1,
            user_list: [],
            goods_list: [],
            goodsRadio: '',
            hasUser: false,
            userInfo: {},
            hasChoose: false,
            chooseGoodsList: [],
            goodsLen: '',
            good_all_list: []
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number',
        },
        {
            title: '账号',
            dataIndex: 'user_username'
        },
        {
            title: '操作',
            dataIndex: this.columns,
            render: data => {
                return (
                    <div>
                        <Button type="danger" size={'small'}
                                onClick={() => this.handleCancel(data.user_id)}>取消</Button>
                    </div>
                )
            }
        },
    ];

    column1s = [
        {
            title: '图片',
            dataIndex: 'file_path',
            render: file_path => {
                return (
                    <img src={config.imgServerUrl + file_path} style={{width: '70px', height: '70px'}}></img>
                )
            }
        },
        {
            title: '商品名',
            dataIndex: 'goods_name'
        },
        {
            title: '售价',
            dataIndex: 'goods_price'
        },
        {
            title: '成本',
            dataIndex: 'goods_cost'
        },
        {
            title: '操作',
            dataIndex: this.column1s,
            render: data => {
                return (
                    <span>
                        {
                            data.hasAdd ?
                                <Button type={'danger'}
                                        onClick={() => this.handleCancelPartIn(data.goods_id)}>取消</Button> :
                                <Button type={'primary'} onClick={() => this.handlePartIn(data)}>加入</Button>
                        }
                    </span>
                )
            }
        }
    ];

    formItemLayout = {
        labelCol: {span: 6},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        const dateFormat = 'YYYY-MM-DD';
        const rowSelection = {
            // 表格中复选框发生变化时
            onChange: (selectedRowKeys, selectedRows) => {
                let positionId = [];
                let positionName = [];
                positionId = selectedRowKeys;
                selectedRows.forEach((item) => {
                    positionName.push(item.role_name);
                });
                console.log(selectedRowKeys);
                this.setState({
                    positionId: positionId,  //批量选中的职位ID
                    positionName: positionName,
                    selectCount: selectedRowKeys.length, //一共选择的职位数量
                })
            },

        };
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>新建红包</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="新建"
                       okText={"提交"}
                       width={'800px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <FormItem label={"红包名称"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_name', {
                                 rules: [{required: true, message: '请输入活动名称'}],
                             })(
                                 <Input
                                     placeholder="请输入活动名称"/>
                             )}
                         </FormItem>
                    <FormItem label={"使用条件"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_condition', {
                                 rules: [{required: true, message: '请输入使用条件'}],
                             })(
                                 <Input
                                     placeholder="请输入使用条件" type={'number'}/>
                             )}
                         </FormItem>
                    <FormItem label={"金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_price', {
                                 rules: [{required: true, message: '请输入金额'}],
                             })(
                                 <Input
                                     placeholder="请输入金额" type={'number'}/>
                             )}
                         </FormItem>
                    <FormItem label={"数量"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_count', {
                                 rules: [{required: true, message: '请输入数量'}],
                             })(
                                 <Input
                                     placeholder="请输入数量" type={'number'}/>
                             )}
                         </FormItem>
                    <FormItem label={"有效期"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_time', {
                                 rules: [{required: true, message: '请选择有效期'}],
                             })(
                                 <RangePicker format={dateFormat} style={{width: '100%'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_explain')(
                                 <TextArea placeholder={'请输入说明'}></TextArea>
                             )}
                         </FormItem>
                    <FormItem label={"用户"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('user', {
                                 rules: [{required: true, message: '请选择用户'}],
                             })(
                                 <RadioGroup onChange={this.onChange} style={{padding: 0}}>
                                     <Radio value={'1'}>所有</Radio>
                                     <Radio value={'2'}>部分</Radio>
                                 </RadioGroup>
                             )}
                         </FormItem>

                    {
                        this.state.value === '2' ?
                            <span>
                                    <FormItem label={"账号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {(
                                 <div style={{display: 'flex'}}>
                                     <Input placeholder={'请输入用户手机回车搜索'} style={{width: 150}}
                                            onChange={(e) => this.handlePhoneOnChange(e)}
                                            onPressEnter={(e) => this.handleOnPress(e)}/>
                                     <Button type={'primary'} style={{marginLeft: 10}}
                                             onClick={() => this.handleAddUser()}>添加</Button>
                                 </div>
                             )}
                         </FormItem>
                    <FormItem label={"已选客户"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {(
                                 <Table
                                     size={'small'}
                                     columns={this.columns}
                                     rowKey={(record, index) => index}
                                     dataSource={this.state.user_list}
                                     loading={this.state.loading}
                                     pagination={false}
                                     onChange={this.handleTableChange}
                                 />
                             )}
                         </FormItem>
                                </span> :
                            ''
                    }
                    <FormItem label={"可用商品"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('goodsRadio', {
                                 rules: [{required: true, message: '请选择商品'}],
                             })(
                                 <RadioGroup onChange={this.handleOnChangeGoods} style={{padding: 0}}>
                                     <Radio value={'1'}>全部</Radio>
                                     <Radio value={'2'}>部分</Radio>
                                 </RadioGroup>
                             )}
                         </FormItem>
                    {
                        this.state.goodsRadio === '2' ? <Row>
                            <Col span={14} offset={4}>
                                <Button type={this.state.hasChoose ? 'primary' : 'default'}
                                        onClick={() => this.handleAddBtn(1)}>已选({this.state.chooseGoodsList.length})</Button>
                                <Button type={this.state.hasChoose ? 'default' : 'primary'}
                                        style={{marginLeft: 20}}
                                        onClick={() => this.handleAddBtn(2)}
                                >未选({this.state.goodsLen - this.state.chooseGoodsList.length})</Button>
                            </Col>
                            <Col span={14} offset={4} style={{marginTop: 20}}>
                                <Table
                                    size={'small'}
                                    columns={this.column1s}
                                    rowKey={(record, index) => index}
                                    dataSource={this.state.goods_list}
                                    loading={this.state.loading}
                                    pagination={false}
                                    onChange={this.handleTableChange}
                                />
                            </Col>
                            <Col span={14} offset={5} style={{marginTop: 20, textAlign: 'right'}}>
                                <Pagination {...this.state.pagination} showSizeChanger showQuickJumper
                                            onChange={(page, pageSize) => this.handleOnChange(page, pageSize)}
                                            onShowSizeChange={(current, size) => this.handleOnShowSizeChange(current, size)}/>
                            </Col>
                        </Row> : ''
                    }
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                let {coupon_time, user, goodsRadio} = values;
                values.coupon_startTime = moment(coupon_time[0]._d).format('YYYY-MM-DD');
                values.coupon_endTime = moment(coupon_time[1]._d).format('YYYY-MM-DD');
                values.coupon_userType = user;
                values.coupon_goodsType = goodsRadio;
                if (user === '2') {
                    let {user_list} = _this.state;
                    if (user_list.length <= 0) {
                        app.alert('请选择用户', 'info', 1);
                        return;
                    }
                    let userList = [];
                    for (let i = 0; i < user_list.length; i++) {
                        let item = {
                            user_id: user_list[i].user_id
                        };
                        userList.push(item);
                    }
                    values.userList = userList;
                }
                if (goodsRadio === '2') {
                    let {chooseGoodsList} = _this.state;
                    if (chooseGoodsList.length <= 0) {
                        app.alert('请选择商品', 'info', 1);
                        return;
                    }
                    let goodsList = [];
                    for (let i = 0; i < chooseGoodsList.length; i++) {
                        let item = {
                            goods_id: chooseGoodsList[i].goods_id
                        };
                        goodsList.push(item);
                    }
                    values.goodsList = goodsList;
                }
                app.post(coupon.ADD_COUPON, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 上传文件
     * @param file
     * @param fileList
     * @param event
     */
    handleUpload(event) {
        const file = event.file;
        const status = file.status;
        const _this = this;
        if (status === "done") {
            const response = file.response;
            if (response.code === 1) {
                const imgUrl = response.data.imgName;
                _this.setState({imgUrl});
            } else {
                app.alert(response.msg, 'error', 1);
                _this.setState({imgUrl: ''});
            }
        }
    }

    onChange = (e) => {
        console.log(e);
        this.setState({value: e.target.value});
    };

    handleOnChangeGoods = (e) => {
        this.setState({goodsRadio: e.target.value});
        if (e.target.value === '2') {
            let {chooseGoodsList} = this.state;
            const param = {
                page: 1,
                size: 10
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    };

    /**
     * 处理页面改变事件
     * @param page
     * @param pageSize
     */
    handleOnChange(page, pageSize) {
        console.log(page, pageSize);
        const {hasChoose, chooseGoodsList} = this.state;
        if (hasChoose) {
            const pagination = {
                page,
                pageSize,
                total: chooseGoodsList.length
            };
            this.setState({pagination});
        } else {
            const param = {
                page,
                size: pageSize
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理pageSize变化回调
     * @param current
     * @param size
     */
    handleOnShowSizeChange(current, size) {
        console.log(current, size);
        const {hasChoose, chooseGoodsList} = this.state;
        if (hasChoose) {
            const pagination = {
                page: current,
                pageSize: size,
                total: chooseGoodsList.length
            };
            this.setState({pagination});
        } else {
            const param = {
                page: current,
                size
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理手机输入事件
     * @param e
     */
    handlePhoneOnChange(e) {
        this.setState({phone: e.target.value});
    }

    /**
     * 处理手机输入回车事件
     * @param e
     */
    handleOnPress(e) {
        console.log(e.target.value);
        let _this = this;
        app.post(user.SEARCH_USER_BY_USERNAME, {user_username: e.target.value}).then((res) => {
            if (res.code === 1) {
                let user_list = _this.state.user_list;
                let {user_id, user_username} = res.data;
                let userInfo = {
                    order_number: user_list.length + 1,
                    user_id,
                    user_username
                };
                _this.setState({userInfo, hasUser: true});
            } else {
                app.alert(res.msg, 'info', 1);
                _this.setState({userInfo: {}, hasUser: false});
            }
        })
    }

    /**
     * 处理添加客户事件
     */
    handleAddUser() {
        let {user_list, userInfo, hasUser} = this.state;
        if (hasUser) {
            let hasAdd = false;
            let user_id = userInfo.user_id;
            for (let i = 0; i < user_list.length; i++) {
                if (user_list[i].user_id === user_id) {
                    hasAdd = true;
                    break;
                }
            }
            if (hasAdd) {
                app.alert('该用户已被添加', 'info', 1);
                return;
            }
            user_list.push(userInfo);
            this.setState({userInfo: {}, user_list, hasUser: false});
        } else {
            app.alert('没有找到用户', 'info', 1);
        }
    }

    /**
     * 处理取消客户事件
     * @param id
     */
    handleCancel(id) {

    }

    /**
     * 查找商品列表
     * @param param
     */
    handleSearchGoodsList(param, chooseGoodsList) {
        const _this = this;
        app.post(goods.SEARCH_GOODS_LIST, param).then((res) => {
            if (res.code === 1) {
                let data = res.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].hasAdd = false;
                    for (let j = 0; j < chooseGoodsList.length; j++) {
                        if (data[i].goods_id === chooseGoodsList[j].goods_id) {
                            data[i].hasAdd = true;
                            break;
                        }
                    }
                }
                const pagination = {current: param.page, pageSize: param.size, total: res.count};
                _this.setState({goods_list: data, pagination, goodsLen: res.count});
            } else {
                app.alert(res.msg, 'info', 1);
                const pagination = {
                    current: 0,
                    pageSize: 0,
                    total: 0
                };
                _this.setState({goods_list: [], pagination, goodsLen: 0});
            }
        })
    }

    /**
     * 已选未选按钮事件
     * @param type
     */
    handleAddBtn(type) {
        if (type === 1) {
            this.setState({hasChoose: true});
            let {chooseGoodsList} = this.state;
            const pagination = {
                page: 1,
                size: 10,
                total: chooseGoodsList.length
            };
            this.setState({goods_list: chooseGoodsList, pagination});
        } else {
            this.setState({hasChoose: false});
            const param = {
                page: 1,
                size: 10
            };
            let {chooseGoodsList} = this.state;
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理加入商品按钮事件
     * @param data
     */
    handlePartIn(data) {
        let {goods_list, chooseGoodsList} = this.state;
        for (let i = 0; i < goods_list.length; i++) {
            if (goods_list[i].goods_id === data.goods_id) {
                goods_list[i].hasAdd = true;
                chooseGoodsList.push(goods_list[i]);
                break;
            }
        }
        this.setState({goods_list, chooseGoodsList});
    };

    /**
     * 取消加入
     * @param id
     */
    handleCancelPartIn(id) {
        let {goods_list, chooseGoodsList} = this.state;
        for (let i = 0; i < goods_list.length; i++) {
            if (goods_list[i].goods_id === id) {
                goods_list[i].hasAdd = false;
                break;
            }
        }
        let currentIndex = -1;
        for (let i = 0; i < chooseGoodsList.length; i++) {
            if (chooseGoodsList[i].goods_id === id) {
                currentIndex = i;
                break;
            }
        }
        if (currentIndex >= 0) {
            chooseGoodsList.splice(currentIndex, 1);
        }
        this.setState({goods_list, chooseGoodsList});
    }

}

const AddRedPackets = Form.create()(AddRedPacket);

class EditRedPacket extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            value: 1,
            user_list: [],
            goods_list: [],
            goodsRadio: '',
            hasUser: false,
            userInfo: {},
            hasChoose: false,
            chooseGoodsList: [],
            goodsLen: '',
            good_all_list: []
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number',
        },
        {
            title: '账号',
            dataIndex: 'user_username'
        }
    ];

    column1s = [
        {
            title: '图片',
            dataIndex: 'file_path',
            render: file_path => {
                return (
                    <img src={config.imgServerUrl + file_path} style={{width: '70px', height: '70px'}}></img>
                )
            }
        },
        {
            title: '商品名',
            dataIndex: 'goods_name'
        },
        {
            title: '售价',
            dataIndex: 'goods_minPrice'
        },
        {
            title: '成本',
            dataIndex: 'goods_minCost'
        }
    ];

    formItemLayout = {
        labelCol: {span: 6},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        const dateFormat = 'YYYY-MM-DD';
        const rowSelection = {
            // 表格中复选框发生变化时
            onChange: (selectedRowKeys, selectedRows) => {
                let positionId = [];
                let positionName = [];
                positionId = selectedRowKeys;
                selectedRows.forEach((item) => {
                    positionName.push(item.role_name);
                });
                console.log(selectedRowKeys);
                this.setState({
                    positionId: positionId,  //批量选中的职位ID
                    positionName: positionName,
                    selectCount: selectedRowKeys.length, //一共选择的职位数量
                })
            },

        };
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>查看</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="查看"
                       okText={"确定"}
                       width={'800px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <FormItem label={"红包名称"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_name', {
                                 rules: [{required: true, message: '请输入活动名称'}],
                             })(
                                 <Input
                                     placeholder="请输入活动名称" disabled={true}/>
                             )}
                         </FormItem>
                    <FormItem label={"使用条件"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_condition', {
                                 rules: [{required: true, message: '请输入使用条件'}],
                             })(
                                 <Input
                                     placeholder="请输入使用条件" type={'number'} disabled={true}/>
                             )}
                         </FormItem>
                    <FormItem label={"金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_price', {
                                 rules: [{required: true, message: '请输入金额'}],
                             })(
                                 <Input
                                     placeholder="请输入金额" type={'number'} disabled={true}/>
                             )}
                         </FormItem>
                    <FormItem label={"数量"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_count', {
                                 rules: [{required: true, message: '请输入数量'}],
                             })(
                                 <Input
                                     placeholder="请输入数量" type={'number'} disabled={true}/>
                             )}
                         </FormItem>
                    <FormItem label={"有效期"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_time', {
                                 rules: [{required: true, message: '请选择有效期'}],
                             })(
                                 <RangePicker format={dateFormat} style={{width: '100%'}} disabled={true}/>
                             )}
                         </FormItem>
                    <FormItem label={"说明"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('coupon_explain')(
                                 <TextArea placeholder={'请输入说明'} disabled={true}></TextArea>
                             )}
                         </FormItem>
                    <FormItem label={"用户"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('user', {
                                 rules: [{required: true, message: '请选择用户'}],
                             })(
                                 <RadioGroup onChange={this.onChange} style={{padding: 0}}>
                                     <Radio value={'1'} disabled={true}>所有</Radio>
                                     <Radio value={'2'} disabled={true}>部分</Radio>
                                 </RadioGroup>
                             )}
                         </FormItem>

                    {
                        this.state.value === '2' ?
                            <span>
                                    <FormItem label={"账号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {(
                                 <div style={{display: 'flex'}}>
                                     <Input placeholder={'请输入用户手机回车搜索'} style={{width: 150}}
                                            onChange={(e) => this.handlePhoneOnChange(e)}
                                            onPressEnter={(e) => this.handleOnPress(e)} disabled={true}/>
                                     <Button type={'primary'} style={{marginLeft: 10}}
                                             onClick={() => this.handleAddUser()} disabled={true}>添加</Button>
                                 </div>
                             )}
                         </FormItem>
                    <FormItem label={"已选客户"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {(
                                 <Table
                                     size={'small'}
                                     columns={this.columns}
                                     rowKey={(record, index) => index}
                                     dataSource={this.state.user_list}
                                     loading={this.state.loading}
                                     pagination={false}
                                     onChange={this.handleTableChange}
                                 />
                             )}
                         </FormItem>
                                </span> :
                            ''
                    }
                    <FormItem label={"可用商品"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('goodsRadio', {
                                 rules: [{required: true, message: '请选择商品'}],
                             })(
                                 <RadioGroup onChange={this.handleOnChangeGoods} style={{padding: 0}}>
                                     <Radio value={'1'} disabled={true}>全部</Radio>
                                     <Radio value={'2'} disabled={true}>部分</Radio>
                                 </RadioGroup>
                             )}
                         </FormItem>
                    {
                        this.state.goodsRadio === '2' ? <Row>
                            <Col span={14} offset={4}>
                                <Button type={this.state.hasChoose ? 'primary' : 'default'}
                                        onClick={() => this.handleAddBtn(1)}>已选({this.state.chooseGoodsList.length})</Button>
                                <Button type={this.state.hasChoose ? 'default' : 'primary'}
                                        style={{marginLeft: 20}}
                                        onClick={() => this.handleAddBtn(2)}
                                >未选({this.state.goodsLen - this.state.chooseGoodsList.length})</Button>
                            </Col>
                            <Col span={14} offset={4} style={{marginTop: 20}}>
                                <Table
                                    size={'small'}
                                    columns={this.column1s}
                                    rowKey={(record, index) => index}
                                    dataSource={this.state.goods_list}
                                    loading={this.state.loading}
                                    pagination={false}
                                    onChange={this.handleTableChange}
                                />
                            </Col>
                            <Col span={14} offset={5} style={{marginTop: 20, textAlign: 'right'}}>
                                <Pagination {...this.state.pagination} showSizeChanger showQuickJumper
                                            onChange={(page, pageSize) => this.handleOnChange(page, pageSize)}
                                            onShowSizeChange={(current, size) => this.handleOnShowSizeChange(current, size)}/>
                            </Col>
                        </Row> : ''
                    }
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            this.handleSearchCouponList(this.props.id);
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                let {coupon_time, user, goodsRadio} = values;
                values.coupon_startTime = moment(coupon_time[0]._d).format('YYYY-MM-DD');
                values.coupon_endTime = moment(coupon_time[1]._d).format('YYYY-MM-DD');
                values.coupon_userType = user;
                values.coupon_goodsType = goodsRadio;
                if (user === '2') {
                    let {user_list} = _this.state;
                    if (user_list.length <= 0) {
                        app.alert('请选择用户', 'info', 1);
                        return;
                    }
                    let userList = [];
                    for (let i = 0; i < user_list.length; i++) {
                        let item = {
                            user_id: user_list[i].user_id
                        };
                        userList.push(item);
                    }
                    values.userList = userList;
                }
                if (goodsRadio === '2') {
                    let {chooseGoodsList} = _this.state;
                    if (chooseGoodsList.length <= 0) {
                        app.alert('请选择商品', 'info', 1);
                        return;
                    }
                    let goodsList = [];
                    for (let i = 0; i < chooseGoodsList.length; i++) {
                        let item = {
                            goods_id: chooseGoodsList[i].goods_id
                        };
                        goodsList.push(item);
                    }
                    values.goodsList = goodsList;
                }
                app.post(coupon.ADD_COUPON, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 2);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 查看单个红包详情
     * @param id
     */
    handleSearchCouponList(id) {
        const _this = this;
        app.post(coupon.SEARCH_COUPON_ONE, {coupon_id: id}).then((res) => {
            if (res.code === 1) {
                console.log(res);
                const {
                    coupon_name,
                    coupon_condition,
                    coupon_count,
                    coupon_endTime,
                    coupon_explain,
                    coupon_goodsType,
                    coupon_price,
                    coupon_startTime,
                    coupon_userType,
                    userList,
                    goodsList
                } = res.data;
                this.setState({goodsRadio: coupon_goodsType, value: coupon_userType});
                let formData = [];
                formData['coupon_name'] = {value: coupon_name};
                formData['coupon_condition'] = {value: coupon_condition};
                formData['coupon_count'] = {value: coupon_count};
                formData['coupon_explain'] = {value: coupon_explain};
                formData['goodsRadio'] = {value: coupon_goodsType};
                formData['user'] = {value: coupon_userType};
                formData['coupon_price'] = {value: coupon_price};
                formData['coupon_time'] = {value: [moment(coupon_startTime), moment(coupon_endTime)]};
                _this.props.form.setFields(formData);
                if (userList) {
                    _this.setState({user_list: userList});
                }
                if (goodsList) {
                    _this.setState({goods_list: goodsList});
                }
            } else {
                app.alert(res.msg, 'info', 1);
            }
        })
    }

    /**
     * 上传文件
     * @param file
     * @param fileList
     * @param event
     */
    handleUpload(event) {
        const file = event.file;
        const status = file.status;
        const _this = this;
        if (status === "done") {
            const response = file.response;
            if (response.code === 1) {
                const imgUrl = response.data.imgName;
                _this.setState({imgUrl});
            } else {
                app.alert(response.msg, 'error', 1);
                _this.setState({imgUrl: ''});
            }
        }
    }

    onChange = (e) => {
        console.log(e);
        this.setState({value: e.target.value});
    };

    handleOnChangeGoods = (e) => {
        this.setState({goodsRadio: e.target.value});
        if (e.target.value === '2') {
            let {chooseGoodsList} = this.state;
            const param = {
                page: 1,
                size: 10
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    };

    /**
     * 处理页面改变事件
     * @param page
     * @param pageSize
     */
    handleOnChange(page, pageSize) {
        console.log(page, pageSize);
        const {hasChoose, chooseGoodsList} = this.state;
        if (hasChoose) {
            const pagination = {
                page,
                pageSize,
                total: chooseGoodsList.length
            };
            this.setState({pagination});
        } else {
            const param = {
                page,
                size: pageSize
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理pageSize变化回调
     * @param current
     * @param size
     */
    handleOnShowSizeChange(current, size) {
        console.log(current, size);
        const {hasChoose, chooseGoodsList} = this.state;
        if (hasChoose) {
            const pagination = {
                page: current,
                pageSize: size,
                total: chooseGoodsList.length
            };
            this.setState({pagination});
        } else {
            const param = {
                page: current,
                size
            };
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理手机输入事件
     * @param e
     */
    handlePhoneOnChange(e) {
        this.setState({phone: e.target.value});
    }

    /**
     * 处理手机输入回车事件
     * @param e
     */
    handleOnPress(e) {
        console.log(e.target.value);
        let _this = this;
        app.post(user.SEARCH_USER_BY_USERNAME, {user_username: e.target.value}).then((res) => {
            if (res.code === 1) {
                let user_list = _this.state.user_list;
                let {user_id, user_username} = res.data;
                let userInfo = {
                    order_number: user_list.length + 1,
                    user_id,
                    user_username
                };
                _this.setState({userInfo, hasUser: true});
            } else {
                app.alert(res.msg, 'info', 1);
                _this.setState({userInfo: {}, hasUser: false});
            }
        })
    }

    /**
     * 处理添加客户事件
     */
    handleAddUser() {
        let {user_list, userInfo, hasUser} = this.state;
        if (hasUser) {
            let hasAdd = false;
            let user_id = userInfo.user_id;
            for (let i = 0; i < user_list.length; i++) {
                if (user_list[i].user_id === user_id) {
                    hasAdd = true;
                    break;
                }
            }
            if (hasAdd) {
                app.alert('该用户已被添加', 'info', 1);
                return;
            }
            user_list.push(userInfo);
            this.setState({userInfo: {}, user_list, hasUser: false});
        } else {
            app.alert('没有找到用户', 'info', 1);
        }
    }

    /**
     * 处理取消客户事件
     * @param id
     */
    handleCancel(id) {

    }

    /**
     * 查找商品列表
     * @param param
     */
    handleSearchGoodsList(param, chooseGoodsList) {
        const _this = this;
        app.post(goods.SEARCH_GOODS_LIST, param).then((res) => {
            if (res.code === 1) {
                let data = res.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].hasAdd = false;
                    for (let j = 0; j < chooseGoodsList.length; j++) {
                        if (data[i].goods_id === chooseGoodsList[j].goods_id) {
                            data[i].hasAdd = true;
                            break;
                        }
                    }
                }
                const pagination = {current: param.page, pageSize: param.size, total: res.count};
                _this.setState({goods_list: data, pagination, goodsLen: res.count});
            } else {
                app.alert(res.msg, 'info', 1);
                const pagination = {
                    current: 0,
                    pageSize: 0,
                    total: 0
                };
                _this.setState({goods_list: [], pagination, goodsLen: 0});
            }
        })
    }

    /**
     * 已选未选按钮事件
     * @param type
     */
    handleAddBtn(type) {
        if (type === 1) {
            this.setState({hasChoose: true});
            let {chooseGoodsList} = this.state;
            const pagination = {
                page: 1,
                size: 10,
                total: chooseGoodsList.length
            };
            this.setState({goods_list: chooseGoodsList, pagination});
        } else {
            this.setState({hasChoose: false});
            const param = {
                page: 1,
                size: 10
            };
            let {chooseGoodsList} = this.state;
            this.handleSearchGoodsList(param, chooseGoodsList);
        }
    }

    /**
     * 处理加入商品按钮事件
     * @param data
     */
    handlePartIn(data) {
        let {goods_list, chooseGoodsList} = this.state;
        for (let i = 0; i < goods_list.length; i++) {
            if (goods_list[i].goods_id === data.goods_id) {
                goods_list[i].hasAdd = true;
                chooseGoodsList.push(goods_list[i]);
                break;
            }
        }
        this.setState({goods_list, chooseGoodsList});
    };

    /**
     * 取消加入
     * @param id
     */
    handleCancelPartIn(id) {
        let {goods_list, chooseGoodsList} = this.state;
        for (let i = 0; i < goods_list.length; i++) {
            if (goods_list[i].goods_id === id) {
                goods_list[i].hasAdd = false;
                break;
            }
        }
        let currentIndex = -1;
        for (let i = 0; i < chooseGoodsList.length; i++) {
            if (chooseGoodsList[i].goods_id === id) {
                currentIndex = i;
                break;
            }
        }
        if (currentIndex >= 0) {
            chooseGoodsList.splice(currentIndex, 1);
        }
        this.setState({goods_list, chooseGoodsList});
    }

}

const EditRedPackets = Form.create()(EditRedPacket);

//添加供应商
class AddSupplier extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>添加供应商</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="添加供应商"
                       okText={"提交"}
                       width={'600px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                    <FormItem label={"供应商编号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_code', {
                                 rules: [{required: true, message: '请输入供应商编号'}],
                             })(
                                 <Input
                                     placeholder="请输入供应商编号" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                         <FormItem label={"供应商名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_name', {
                                 rules: [{required: true, message: '请输入供应商名'}],
                             })(
                                 <Input
                                     placeholder="请输入供应商名" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"详细地址"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_address', {
                                 rules: [{required: true, message: '请输入详细地址'}],
                             })(
                                 <TextArea
                                     placeholder="请输入详细地址" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"联系人"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_contact', {
                                 rules: [{required: true, message: '请输入联系人'}],
                             })(
                                 <Input
                                     placeholder="请输入联系人" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"联系方式"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_phone', {
                                 rules: [{required: true, message: '请输入联系方式'}],
                             })(
                                 <Input
                                     placeholder="请输入联系方式" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"简介"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_text')(
                                 <TextArea
                                     placeholder="请输入简介" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                app.post(supplier.ADD_SUPPLIER, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 1);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        })
    }
}

const AddSuppliers = Form.create()(AddSupplier);

//编辑供应商
class EditSupplier extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>编辑</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="编辑供应商"
                       okText={"提交"}
                       width={'600px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"供应商编号"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_code', {
                                 rules: [{required: true, message: '请输入供应商编号'}],
                             })(
                                 <Input
                                     placeholder="请输入供应商编号" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                         <FormItem label={"供应商名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_name', {
                                 rules: [{required: true, message: '请输入供应商名'}],
                             })(
                                 <Input
                                     placeholder="请输入供应商名" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"详细地址"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_address', {
                                 rules: [{required: true, message: '请输入详细地址'}],
                             })(
                                 <TextArea
                                     placeholder="请输入详细地址" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"联系人"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_contact', {
                                 rules: [{required: true, message: '请输入联系人'}],
                             })(
                                 <Input
                                     placeholder="请输入联系人" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"联系方式"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_phone', {
                                 rules: [{required: true, message: '请输入联系方式'}],
                             })(
                                 <Input
                                     placeholder="请输入联系方式" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                    <FormItem label={"简介"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('supplier_text')(
                                 <TextArea
                                     placeholder="请输入简介" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            let data = this.props.data;
            let formData = [];
            formData['supplier_code'] = {value: data.supplier_code};
            formData['supplier_name'] = {value: data.supplier_name};
            formData['supplier_address'] = {value: data.supplier_address};
            formData['supplier_contact'] = {value: data.supplier_contact};
            formData['supplier_phone'] = {value: data.supplier_phone};
            formData['supplier_text'] = {value: data.supplier_text};
            this.props.form.setFields(formData);
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                values.supplier_id = _this.props.data.supplier_id;
                app.post(supplier.UPDATE_SUPPLIER, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 1);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        })
    }
}

const EditSuppliers = Form.create()(EditSupplier);


//添加属性
class AddSku extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            visible1: false,
            valueData: [
                {
                    value: ''
                }
            ]
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>新建属性</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="新建属性"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       footer={
                           <div>
                               <Button type={'primary'} onClick={() => this.handleSave()}>保存</Button>
                               <Button type={'primary'} onClick={() => this.handleAddValue(true)}>下一步添加属性值</Button>
                           </div>
                       }
                >
                         <FormItem label={"属性名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('sku_property_name', {
                                 rules: [{required: true, message: '请输入属性名'}],
                             })(
                                 <Input
                                     placeholder="请输入属性名" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                </Modal>

                <Modal visible={this.state.visible1}
                       maskClosable={false}
                       title="新建属性"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.handleHideVisible(false)}
                       footer={
                           <div>
                               <Button type={'primary'} onClick={() => this.handleShowLast()}>上一步</Button>
                               <Button type={'primary'} onClick={(e) => this.handleSubmit(e)}>提交</Button>
                           </div>
                       }
                >
                    {
                        this.state.valueData.map((item, index) => {
                            return (
                                <FormItem label={"属性值"} style={{display: 'flex'}} {...this.formItemLayout} key={index}>
                                    {getFieldDecorator('value' + index, {
                                        rules: [{required: true, message: '请输入属性值'}],
                                    })(
                                        <div style={{display: 'flex'}}>
                                            <Input
                                                placeholder="请输入属性值" style={{width: '200px'}} value={item.value}
                                                onChange={(e) => this.handleValueOnChange(e, index)}/>
                                            {
                                                index + 1 === this.state.valueData.length ?
                                                    <Button icon={'plus'} type={'primary'} style={{marginLeft: 5}}
                                                            onClick={() => this.handleAddSkuValue(index)}></Button>
                                                    :
                                                    <Button icon={'minus'} type={'danger'}
                                                            style={{marginLeft: 5}}
                                                            onClick={() => this.handleDeleteSkuValue(index)}></Button>
                                            }
                                        </div>
                                    )}
                                </FormItem>
                            )
                        })
                    }
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (!visible) {
            this.props.form.resetFields();
        }
        this.setState({visible: visible});
    }

    handleHideVisible() {
        this.props.form.resetFields();
        this.setState({visible: false, visible1: false});
    }

    /**
     * 上一步
     */
    handleShowLast() {
        this.setState({
            visible: true,
            visible1: false
        })
    }

    /**
     * 添加属性值
     * @param visible
     */
    handleAddValue(visible) {
        this.props.form.validateFields(['sku_property_name'], (error, values) => {
            if (!error) {
                this.setState({visible1: visible, visible: false});
            }
        });
    }

    /**
     * 添加属性值
     * @param index
     */
    handleAddSkuValue(index) {
        const _this = this;
        const {valueData} = this.state;
        if (valueData.length > 1) {
            let isRepeat = false;
            let lastValue = valueData[valueData.length - 1].value;
            for (let i = 0; i < valueData.length - 1; i++) {
                lastValue = lastValue.replace(/\s+/g, '');
                valueData[i].value = valueData[i].value.replace(/\s+/g, '');
                if (valueData[i].value === lastValue) {
                    isRepeat = true;
                    break;
                }
            }
            if (isRepeat) {
                app.alert('属性值不能重复', 'info', 1);
                return;
            }
        }
        this.props.form.validateFields((error, values) => {
            if (!error) {
                let valueData = _this.state.valueData;
                valueData.push({value: ''});
                _this.setState({valueData});
            }
        });
    }

    /**
     * 删除属性值
     * @param index
     */
    handleDeleteSkuValue(index) {
        let valueData = this.state.valueData;
        valueData.splice(index, 1);
        this.setState({valueData});
    }

    /**
     * 处理属性值输入变化事件
     * @param e
     * @param index
     */
    handleValueOnChange(e, index) {
        let valueData = this.state.valueData;
        valueData[index].value = e.target.value;
        this.setState({valueData});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log(values);
                console.log(this.state.valueData);
                let valueData = _this.state.valueData;
                let recordList = [];
                for (let i = 0; i < valueData.length; i++) {
                    let item = {
                        sku_record_name: valueData[i].value
                    };
                    recordList.push(item);
                }
                values.recordList = recordList;
                app.post(sku.ADD_SKU, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 1);
                        _this.setState({
                            visible: false,
                            visible1: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                        _this.setState({valueData: [{value: ''}]});
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        })
    }

    /**
     * 保存属性名
     */
    handleSave() {
        const _this = this;
        this.props.form.validateFields(['sku_property_name'], (error, values) => {
            if (!error) {
                app.post(sku.ADD_SKU, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 1);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        });
    }

}

const AddSkus = Form.create()(AddSku);

//编辑属性
class EditSku extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            visible1: false,
            valueData: []
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)}>编辑</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="编辑属性"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       footer={
                           <div>
                               <Button type={'primary'} onClick={() => this.handleSave()}>保存</Button>
                               <Button type={'primary'} onClick={() => this.handleAddValue(true)}>下一步编辑属性值</Button>
                           </div>
                       }
                >
                         <FormItem label={"属性名"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('sku_property_name', {
                                 rules: [{required: true, message: '请输入属性名'}],
                             })(
                                 <Input
                                     placeholder="请输入属性名" style={{width: '250px'}}/>
                             )}
                         </FormItem>
                </Modal>

                <Modal visible={this.state.visible1}
                       maskClosable={false}
                       title="编辑属性"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.handleHideVisible(false)}
                       footer={
                           <div>
                               <Button type={'primary'} onClick={() => this.handleShowLast()}>上一步</Button>
                               <Button type={'primary'} onClick={(e) => this.handleSubmit(e)}>提交</Button>
                           </div>
                       }
                >
                    {
                        this.state.valueData.map((item, index) => {
                            return (
                                <FormItem label={"属性值"} style={{display: 'flex'}} {...this.formItemLayout} key={index}>
                                    {getFieldDecorator('value' + index, {
                                        rules: [{required: true, message: '请输入属性值'}],
                                    })(
                                        <div style={{display: 'flex'}}>
                                            <Input
                                                placeholder="请输入属性值" style={{width: '200px'}} value={item.value}
                                                onChange={(e) => this.handleValueOnChange(e, index)}/>
                                            {
                                                index + 1 === this.state.valueData.length ?
                                                    <Button icon={'plus'} type={'primary'} style={{marginLeft: 5}}
                                                            onClick={() => this.handleAddSkuValue(index)}></Button>
                                                    :
                                                    <Button icon={'minus'} type={'danger'}
                                                            style={{marginLeft: 5}}
                                                            onClick={() => this.handleDeleteSkuValue(index)}></Button>
                                            }
                                        </div>
                                    )}
                                </FormItem>
                            )
                        })
                    }
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        if (!visible) {
            this.props.form.resetFields();
        }
        if (visible) {
            this.handleSearchSkuOne();
        }
        this.setState({visible: visible});
    }

    handleHideVisible() {
        this.props.form.resetFields();
        this.setState({visible: false, visible1: false});
    }

    /**
     * 上一步
     */
    handleShowLast() {
        this.setState({
            visible: true,
            visible1: false
        })
    }

    /**
     * 添加属性值
     * @param visible
     */
    handleAddValue(visible) {
        this.props.form.validateFields(['sku_name'], (error, values) => {
            if (!error) {
                this.setState({visible1: visible, visible: false});
            }
        });
    }

    /**
     * 添加属性值
     * @param index
     */
    handleAddSkuValue(index) {
        const _this = this;
        const {valueData} = this.state;
        if (valueData.length > 1) {
            let isRepeat = false;
            let lastValue = valueData[valueData.length - 1].value;
            for (let i = 0; i < valueData.length - 1; i++) {
                lastValue = lastValue.replace(/\s+/g, '');
                valueData[i].value = valueData[i].value.replace(/\s+/g, '');
                if (valueData[i].value === lastValue) {
                    isRepeat = true;
                    break;
                }
            }
            if (isRepeat) {
                app.alert('属性值不能重复', 'info', 1);
                return;
            }
        }
        this.props.form.validateFields((error, values) => {
            if (!error) {
                let valueData = _this.state.valueData;
                valueData.push({value: ''});
                _this.setState({valueData});
            }
        });
    }

    /**
     * 删除属性值
     * @param index
     */
    handleDeleteSkuValue(index) {
        let valueData = this.state.valueData;
        valueData.splice(index, 1);
        this.setState({valueData});
    }

    /**
     * 处理属性值输入变化事件
     * @param e
     * @param index
     */
    handleValueOnChange(e, index) {
        let valueData = this.state.valueData;
        valueData[index].value = e.target.value;
        this.setState({valueData});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                let valueData = _this.state.valueData;
                let recordList = [];
                for (let i = 0; i < valueData.length; i++) {
                    let item = {
                        sku_record_name: valueData[i].value
                    };
                    recordList.push(item);
                }
                values.recordList = recordList;
                values.sku_property_id = _this.props.data.sku_property_id;
                app.post(sku.UPDATE_SKU, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 1);
                        _this.setState({
                            visible: false,
                            visible1: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        })
    }

    /**
     * 保存属性名
     */
    handleSave() {
        const _this = this;
        this.props.form.validateFields(['sku_property_name'], (error, values) => {
            if (!error) {
                console.log(values);
                values.sku_property_id = _this.props.data.sku_property_id;
                app.post(sku.UPDATE_SKU, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("修改成功", 'success', 1);
                        _this.setState({
                            visible: false
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 1);
                    }
                })
            }
        });
    }

    /**
     * 查找单个属性
     */
    handleSearchSkuOne() {
        const _this = this;
        const sku_property_id = this.props.data.sku_property_id;
        app.post(sku.SEARCH_SKU_ONE, {sku_property_id}).then((res) => {
            if (res.code === 1) {
                const {sku_property_name, recordList} = res.data;
                let formData = [];
                formData['sku_property_name'] = {value: sku_property_name};
                let valueData = [];
                if (recordList) {
                    for (let i = 0; i < recordList.length; i++) {
                        let item = {
                            value: recordList[i].sku_record_name
                        };
                        valueData.push(item);
                    }
                    _this.setState({valueData});
                } else {
                    let item = {
                        value: ''
                    };
                    valueData.push(item);
                }
                _this.setState({valueData});
                for (let i = 0; i < valueData.length; i++) {
                    formData['value' + i] = {value: valueData[i].value};
                }
                _this.props.form.setFields(formData);
            } else {
                app.alert(res.msg, 'info', 1);
            }
        })
    }

}

const EditSkus = Form.create()(EditSku);

class AddRecharge extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState} = this.state;
        return (
            <span>
                <Button type='primary' onClick={() => this.showModal(true)} style={{marginLeft: '20px'}}>新增</Button>
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title="新增充值"
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={"充值金额"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('project_name', {
                                 rules: [{required: true, message: '请输入充值金额'}],
                             })(
                                 <span>
                                 <Input
                                     placeholder="请输入充值金额" style={{width: '180px'}}/><span
                                     className={'sendTimeSpan'}>元</span>
                                 </span>
                             )}
                         </FormItem>
                    <FormItem label={"送"} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('project_name')(
                                 <span>
                                 <Input
                                     placeholder="请输入赠送金额" style={{width: '180px'}}/><span
                                     className={'sendTimeSpan'}>元</span>
                                 </span>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    //弹出关闭添加框
    showModal(visible) {
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        const {imgUrl, newContent} = this.state;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (newContent.length <= 0) {
                    app.alert('请输入简介内容', 'error', 2);
                    return;
                }
                values.project_picture = imgUrl;
                values.project_content = newContent;
                app.post(project.CREATE_PROJECT, values).then((req) => {
                    if (req.code == 1) {
                        app.alert("成功添加", 'success', 2);
                        _this.setState({
                            visible: false,
                            imgUrl: '',
                            newContent: '',
                            editorState: EditorState.createEmpty()
                        });
                        _this.props.form.resetFields();
                        _this.props.refresh();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            }
        })
    }

    /**
     * 上传文件
     * @param file
     * @param fileList
     * @param event
     */
    handleUpload(event) {
        const file = event.file;
        const status = file.status;
        const _this = this;
        if (status === "done") {
            const response = file.response;
            if (response.code === 1) {
                const imgUrl = response.data.imgName;
                _this.setState({imgUrl});
            } else {
                app.alert(response.msg, 'error', 1);
                _this.setState({imgUrl: ''});
            }
        }
    }

}

const AddRecharges = Form.create()(AddRecharge);


class SettingPrice extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            imgUrl: '',
            editorState: EditorState.createEmpty(), //案例介绍
            newContent: '',
            type: 1
        }
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 12},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {editorState, type, typeText} = this.state;
        return (
            <span>
                {
                    type === 1 ?
                        <span style={{color: '#3388ff', marginLeft: '8px', cursor: 'pointer'}}
                              onClick={() => this.showModal(true)}>价格</span>
                        :
                        <span style={{color: '#3388ff', marginLeft: '8px', cursor: 'pointer'}}
                              onClick={() => this.showModal(true)}>库存</span>
                }
                <Modal visible={this.state.visible}
                       maskClosable={false}
                       title={typeText}
                       okText={"提交"}
                       width={'500px'}
                       cancelText={"返回"}
                       onCancel={() => this.showModal(false)}
                       onOk={(e) => this.handleSubmit(e)}
                >
                         <FormItem label={typeText} style={{display: 'flex'}} {...this.formItemLayout}>
                             {getFieldDecorator('num', {
                                 rules: [{required: true, message: '请输入数值'}],
                             })(
                                 <span>
                                 <Input
                                     placeholder="请输入" style={{width: '180px'}} type={'number'}/>
                                 </span>
                             )}
                         </FormItem>
                </Modal>
            </span>
        )
    }

    componentDidMount() {
        let type = this.props.type;
        let typeText = '';
        if (type === 1) {
            typeText = '设置价格';
        } else {
            typeText = '设置库存';
        }
        this.setState({type, typeText});
    }

    //弹出关闭添加框
    showModal(visible) {
        if (visible) {
            if (this.props.data.length <= 0) {
                app.alert('请先选择规格项目', 'info', 1);
                return;
            }
        }
        this.setState({visible: visible});
    }

    //提交添加账户
    handleSubmit(e) {
        e.preventDefault();
        let _this = this;
        this.props.form.validateFields((err, values) => {
            if (!err) {
                if (isNaN(values.num)) {
                    app.alert('请设置为数字类型', 'info', 1);
                    return;
                }
                _this.props.handleGetNum(values.num, _this.state.type);
                _this.showModal(false);
                _this.props.form.resetFields();
            }
        })
    }

}

const SettingPrices = Form.create()(SettingPrice);

export {
    AddAccounts,//添加账号
    EditAccounts,//修改账号
    AddPositions,//增加职位
    EditPositions,//修改职位
    AddAdverts,//新增广告
    EditAdverts,//编辑广告
    ChooseGoods,//选择商品
    AddInvitationActivitys,//新增邀请奖励活动
    EditInvitationActivitys,//修改邀请奖励活动
    CustomerLists,//已邀请列表
    GoodLists,//选择商品
    AddRedPackets,//新增红包
    EditRedPackets,//查看红包
    AddSuppliers,//添加供应商
    EditSuppliers,//编辑供应商
    AddSkus,//添加属性
    EditSkus,//编辑属性
    AddRecharges,//新增充值
    SettingPrices,//批量设置
}